package com.ccf.business.knowledge.controller;

import cn.hutool.core.util.StrUtil;
import com.ccf.business.knowledge.model.vo.LabelTaskVO;
import com.ccf.business.knowledge.service.ILabelTaskService;
import com.ccf.business.knowledge.service.ILabelTaskTypeService;
import com.common.link.model.EntityVO;
import com.common.link.model.RelationAndEntityVO;
import com.common.model.pojo.Query;
import com.common.model.result.PageResult;
import com.common.model.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标注任务
 *
 * @author ccf
 * @date 2022-02-15 16:56:10
 */
@Slf4j
@RestController
@RequestMapping("/labeltask")
@Api(tags = "标注任务相关")
@RequiredArgsConstructor
public class LabelTaskController {

    private final ILabelTaskService labelTaskService;

    private final ILabelTaskTypeService labelTaskTypeService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询列表", notes = "特殊用户列表，可以传不同的参数进入")
    @GetMapping
    public PageResult list(Query query) {
        return labelTaskService.findList(query);
    }


    /**
     * 列表
     */
    @ApiOperation(value = "分页查询标注列表")
    @GetMapping("/labelList")
    public PageResult labelList(Query query) {
        return labelTaskService.labelList(query);
    }


    /**
     * 列表
     */
    @ApiOperation(value = "分页查询审核列表")
    @GetMapping("/auditList")
    public PageResult auditList(Query query) {
        return labelTaskService.auditList(query);
    }


    /**
     * 查询
     */
    @ApiOperation(value = "查询",response = LabelTaskVO.class)
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        LabelTaskVO model = labelTaskService.detail(id);
        return Result.succeed(model, "查询成功");
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody LabelTaskVO labelTask) {
        if (labelTaskService.isExistFieldAndDelFlag(labelTask.getId(), labelTask.getName(), "name")){
            return Result.failed("名称已存在");
        }
        try {
            String id = labelTaskService.saveOrUpdateAll(labelTask);
            return Result.succeed(id,"保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }

    }

    @ApiOperation(value = "提交")
    @PostMapping("/submit")
    public Result submit(@RequestBody LabelTaskVO labelTask) {
        if (labelTaskService.isExistNameAndDelFlag(labelTask.getId(), labelTask.getName())){
            return Result.failed("名称已存在");
        }
        try {
            String id = labelTaskService.saveOrUpdateAll(labelTask);
            labelTaskService.submit(id);
            return Result.succeed(id,"提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }

    }

    @ApiOperation(value = "下发")
    @PostMapping("/send")
    public Result send(@RequestBody LabelTaskVO labelTask) {
        try {
            labelTaskService.submit(labelTask.getId());
            return Result.succeed(labelTask.getId(),"下发成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }

    }

    @ApiOperation(value = "归档")
    @GetMapping("/tidy/{id}")
    public Result tidy(@PathVariable String id) {
        try {
            labelTaskService.dataTidy(id);
            return Result.succeed("归档成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }

    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        //批量删除
        if (StrUtil.contains(id, ",")){
            String[] ids = StrUtil.split(id, ",");
            labelTaskService.logicRemoveByIds(ids);
        } else {
            labelTaskService.logicRemoveByIds(id);
        }
        return Result.succeed("删除成功");
    }

    /**
     * 列表
     */
    @ApiOperation(value = "标注时查询实体类型", notes = "名称模糊查询，前端自行匹配")
    @ApiImplicitParam(value = "任务id", name = "taskId", required = true)
    @GetMapping("/entity")
    public Result entityList(@RequestParam(name = "taskId", required = true) String taskId) {
        try {
            List<EntityVO> resList = labelTaskTypeService.entityList(taskId);
            return Result.succeed(resList, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

    /**
     * 列表
     */
    @ApiOperation(value = "标注时查询关系类型")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "任务id", name = "taskId", required = true),
            @ApiImplicitParam(value = "任务id", name = "sourceId", required = false),
            @ApiImplicitParam(value = "任务id", name = "targetId", required = false)
    })
    @GetMapping("/relation")
    public Result relationList(@RequestParam(name = "taskId", required = true) String taskId,
                               @RequestParam(name = "sourceId", required = false) String sourceId,
                               @RequestParam(name = "targetId", required = false) String targetId
                               ) {
        try {
            List<RelationAndEntityVO> resList = labelTaskTypeService.relationList(taskId, sourceId, targetId);
            return Result.succeed(resList, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }
}
